package com.taptech.doufu.ui.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.taptech.doufu.R
import com.taptech.doufu.ktexpand.setTextWithAt
import com.taptech.doufu.model.novel.bean.ImageBean
import com.taptech.doufu.model.novel.bean.NovelCommentBean
import com.taptech.doufu.model.novel.bean.NovelCommentReplyBean
import com.taptech.doufu.ui.activity.ShowPicActivity
import com.taptech.doufu.ui.activity.UserDetailActivity
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity
import com.taptech.doufu.util.DiaobaoUtil
import com.taptech.doufu.util.GlideUtil
import com.taptech.doufu.util.GsonUtil
import kotlinx.android.synthetic.main.adapter_novel_comment.view.*

class NovelHotCommentAdapter(context:Context, private val authorId:String, private val onClickPraiseListener: OnClickPraiseListener):BaseRecyclerAdappter<NovelCommentBean,NovelHotCommentAdapter.ViewHolder>(context){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.adapter_novel_comment, null)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val item = dataList[position]
        GlideUtil.displayCircleImage(holder.itemView.ivUserHead, item.user.icon)
        GlideUtil.displayCircleImage(holder.itemView.ivUserMedal, item.user.medal)
        holder.itemView.layUserGrade.setData(item.user,authorId == item.user.userId)
        holder.itemView.tvUpdateTime.text = "${DiaobaoUtil.seconds2TimeString(item.update_time)} " +
                "${if (item.article!=null&&item.article.origin_title.isNotEmpty()) " . ${item.article.origin_title}" else ""}"
        holder.itemView.tvPraise.text = "${item.like_times}"
        //设置评论图片
        if (item.content_image!=null && item.content_image!="") {
            val image = GsonUtil.parseJsonWithGson(JSON.toJSONString(item.content_image),ImageBean::class.java)
            if (image!=null){
                val imgWidth = (if (image.width>200) 200 else image.width)
                val imgHeight = image.height.toFloat()/image.width.toFloat() * imgWidth
                GlideUtil.displayImage(holder.itemView.ivCommentImage, image.url,imgWidth,imgHeight.toInt())
                holder.itemView.ivCommentImage.visibility = View.VISIBLE
            }else{
                holder.itemView.ivCommentImage.visibility = View.GONE
            }
            holder.itemView.ivCommentImage.setOnClickListener {
                ShowPicActivity.showPic(mContext,image.url ,0)
            }
        }else{
            holder.itemView.ivCommentImage.visibility = View.GONE
        }
        if (item.comment_content.isNullOrEmpty()) {
            holder.itemView.tvCommentContent.visibility = View.GONE
        }else{
            holder.itemView.tvCommentContent.visibility = View.VISIBLE
            holder.itemView.tvCommentContent.setTextWithAt(item.comment_content)
        }

        if (item.has_liked > 0) {
            holder.itemView.ivPraise.setImageResource(R.drawable.icon_like_select)
            holder.itemView.tvPraise.setTextColor(Color.parseColor("#fe6f6f"))
        }else{
            holder.itemView.ivPraise.setImageResource(R.drawable.icon_like)
            holder.itemView.tvPraise.setTextColor(Color.parseColor("#999999"))
        }


        holder.itemView.layPraise.setOnClickListener {
            onClickPraiseListener.onClick(item.id,position,item.has_liked != 0)
        }

        holder.itemView.ivUserHead.setOnClickListener {
            UserDetailActivity.startActivity(mContext,item.user.userId)
        }
        holder.itemView.setOnClickListener {
            jumpToCommentDetail(item.id)
        }
//        holder.itemView.tvCommentContent.setOnClickListener {
//            jumpToCommentDetail(item.id)
//        }
//        holder.itemView.layReply.setOnClickListener {
//            jumpToCommentDetail(item.id)
//        }

        when {
            item.replies.size==0 -> holder.itemView.layReply.visibility = View.GONE
            item.replies.size == 1 -> {
                holder.itemView.tvReplyOne.setTextWithAt(getReplyContent(item.replies[0]))
                holder.itemView.tvReplyTwo.visibility = View.GONE
                holder.itemView.layReply.visibility = View.VISIBLE
            }
            else -> {
                holder.itemView.tvReplyOne.setTextWithAt(getReplyContent(item.replies[0]))
                holder.itemView.tvReplyTwo.setTextWithAt(getReplyContent(item.replies[1]))
                holder.itemView.layReply.visibility = View.VISIBLE
            }
        }
        
        if (item.reply_times > 2) {
            holder.itemView.tvReplyCount.text = "共${item.reply_times}条回复 >"
            holder.itemView.tvReplyCount.visibility = View.VISIBLE
        }else{
            holder.itemView.tvReplyCount.visibility = View.GONE
        }
    }


    private fun jumpToCommentDetail(id:Int) {
        val url = "TFComment/TFCommentDetail.js"
        val map = HashMap<String, Any>()
        map["id"] = id
        SimpleWeexActivity.startActivity(mContext,url,map)
    }

    private fun getReplyContent(reply: NovelCommentReplyBean):String {
        return "${if (reply.user!=null&& reply.user.nickname!=null  && reply.user.nickname.isNotEmpty()) reply.user.nickname else ""}" +
                "${if (reply.to_user!=null && reply.to_user.nickname!=null &&  reply.to_user.nickname.isNotEmpty()) " 回复 ${reply.to_user.nickname}" else "" }" +
                ":${reply.content} ${if (reply.content_image.isNullOrEmpty()) "" else "【图片】"}"
    }


    interface OnClickPraiseListener{
        fun onClick(id:Int,position: Int,cancel:Boolean)
    }

    class ViewHolder(view:View): RecyclerView.ViewHolder(view)
}